package cn.com.woxitech.dev.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.woxitech.dev.dao.gen.WmFormFieldMapper;
import cn.com.woxitech.dev.dao.gen.WmFormMapper;
import cn.com.woxitech.dev.dao.gen.WmFormRecordMapper;
import cn.com.woxitech.dev.dao.gen.WmFormRoomMapper;
import cn.com.woxitech.dev.model.gen.WmForm;
import cn.com.woxitech.dev.model.gen.WmFormExample;
import cn.com.woxitech.dev.model.gen.WmFormField;
import cn.com.woxitech.dev.model.gen.WmFormFieldExample;
import cn.com.woxitech.dev.model.gen.WmFormRecord;
import cn.com.woxitech.dev.model.gen.WmFormRecordExample;
import cn.com.woxitech.dev.model.gen.WmFormRoom;
import cn.com.woxitech.dev.model.gen.WmFormRoomExample;

@Service
public class FormService {
	@Autowired
	private WmFormMapper wmFormMapper;
	@Autowired
	private WmFormFieldMapper wmFormFieldMapper;
	@Autowired
	private WmFormRecordMapper wmFormRecordMapper;
	@Autowired
	private WmFormRoomMapper wmFormRoomMapper;
	
	public List<WmForm> getAllForms() {
		// TODO Auto-generated method stub
		WmFormExample e = new WmFormExample();
		return wmFormMapper.selectByExample(e);
	}

	public List<WmFormField> getAllFieldsByFormid(String formid) {
		// TODO Auto-generated method stub
		WmFormFieldExample e = new WmFormFieldExample();
		e.createCriteria().andFormidEqualTo(formid);
		e.setOrderByClause("orderNum asc");
		return wmFormFieldMapper.selectByExample(e);
	}

	public List<WmFormRecord> getRecordsByFormid(String formid) {
		// TODO Auto-generated method stub
		WmFormRecordExample e = new WmFormRecordExample();
		e.createCriteria().andFormidEqualTo(formid);
		return wmFormRecordMapper.selectByExample(e);
	}

	public List<WmFormRecord> getRecordsByFormidAndFieldid(String formid, String fieldid) {
		// TODO Auto-generated method stub
		WmFormRecordExample e = new WmFormRecordExample();
		e.createCriteria().andFormidEqualTo(formid).andFieldidEqualTo(fieldid);
		return wmFormRecordMapper.selectByExample(e);
	}

	public String getFormIdByRoomId(String roomid) {
		// TODO Auto-generated method stub
		WmFormRoomExample e = new WmFormRoomExample();
		e.createCriteria().andRoomidEqualTo(roomid);
		List<WmFormRoom> list =  wmFormRoomMapper.selectByExample(e);
		
		if(list == null || list.isEmpty()){
			return null;
		}else{
			return list.get(0).getFormid();	
		}
		
		
	}

	public WmFormRecord getFormRecordByFieldValue(String formid, String val) {
		// TODO Auto-generated method stub
		WmFormRecordExample e = new WmFormRecordExample();
		e.createCriteria().andFormidEqualTo(formid).andFieldvalueEqualTo(val);
		List<WmFormRecord> list = wmFormRecordMapper.selectByExample(e);
		if(list == null || list.isEmpty()){
			return null;
		}else{
			return list.get(0);
		}
	}

	public List<WmFormRecord> getFormRecordByRecordId(String recordid) {
		// TODO Auto-generated method stub
		return null;
	}

	public void bindFormRoomById(String formid, String roomid) {
		// TODO Auto-generated method stub
		WmFormRoomExample e = new WmFormRoomExample();
		e.createCriteria().andFormidEqualTo(formid).andRoomidEqualTo(roomid);
		int count = wmFormRoomMapper.countByExample(e);
		if(count == 0){
			WmFormRoom wfr = new WmFormRoom();
			wfr.setFormid(formid);
			wfr.setRoomid(roomid);
			wfr.setTime(new Date(System.currentTimeMillis()));
			wmFormRoomMapper.insertSelective(wfr);			
		}
	}

	public void saveRecords(List<WmFormRecord> list) {
		// TODO Auto-generated method stub
		
		for(int i=0; i< list.size();i++){
			wmFormRecordMapper.insertSelective(list.get(i));
		}
		
	}

	public void updateRecords(List<WmFormRecord> recordList) {
		// TODO Auto-generated method stub
		
		if(recordList != null && !recordList.isEmpty()){
			String formid = recordList.get(0).getFormid();
			String recordid = recordList.get(0).getRecordid();
			
			for(int i=0; i< recordList.size();i++){
				WmFormRecord r = recordList.get(i);

				WmFormRecordExample e = new WmFormRecordExample();
				e.createCriteria().andFormidEqualTo(formid).andRecordidEqualTo(recordid).andFieldidEqualTo(r.getFieldid());
				wmFormRecordMapper.updateByExampleSelective(r, e);
					
			}	
		}
		
		
	}

	public List<WmFormRoom> getFormRoomListByFormid(String formid) {
		// TODO Auto-generated method stub
		WmFormRoomExample e = new WmFormRoomExample();
		e.createCriteria().andFormidEqualTo(formid);
		return wmFormRoomMapper.selectByExample(e);
	}

	public void unbindFormRoomById(String formid, String roomid) {
		// TODO Auto-generated method stub
		WmFormRoomExample e = new WmFormRoomExample();
		e.createCriteria().andFormidEqualTo(formid).andRoomidEqualTo(roomid);
		wmFormRoomMapper.deleteByExample(e);
	}

	public WmForm getFormByFormid(String formid) {
		// TODO Auto-generated method stub
		
		WmFormExample e = new WmFormExample();
		e.createCriteria().andFormidEqualTo(formid);
		
		List<WmForm> list =  wmFormMapper.selectByExample(e);
		
		if(list == null || list.isEmpty()){
			return null;
		}else{
			return list.get(0);	
		}

	}

	public void updateForm(WmForm form) {
		// TODO Auto-generated method stub
		wmFormMapper.updateByPrimaryKeySelective(form);
	}

	public void unbindAllFormRoom(String formid) {
		// TODO Auto-generated method stub
		WmFormRoomExample e = new WmFormRoomExample();
		e.createCriteria().andFormidEqualTo(formid);
		wmFormRoomMapper.deleteByExample(e);	
	}

	public void deleteFormRecord(String formid, String recordid) {
		// TODO Auto-generated method stub
		WmFormRecordExample e = new WmFormRecordExample();
		e.createCriteria().andFormidEqualTo(formid).andRecordidEqualTo(recordid);
		wmFormRecordMapper.deleteByExample(e);
		
	}

	public void saveForm(WmForm form) {
		// TODO Auto-generated method stub
		wmFormMapper.insertSelective(form);
		
	}

	public void deleteForm(WmForm form) {
		// TODO Auto-generated method stub
		wmFormMapper.deleteByPrimaryKey(form.getId());
	}

	public List<WmFormField> getFormFieldListByFormid(String formid) {
		// TODO Auto-generated method stub
		WmFormFieldExample e = new WmFormFieldExample();
		e.createCriteria().andFormidEqualTo(formid);
//		e.setOrderByClause(" ordernum asc");
		return wmFormFieldMapper.selectByExample(e);
	}

	public void saveFormField(WmFormField f) {
		// TODO Auto-generated method stub
		wmFormFieldMapper.insertSelective(f);
	}
	
	
}


